package grammarLL1;

import java.util.LinkedList;
import java.util.List;

public class NonTerminal extends Symbol {
	private List<Production> productions; // producciones que comienzan con este no terminal

	public NonTerminal(String symbol) {
		super(symbol);
		this.productions = new LinkedList<Production>();
	}
	
	public void linkProduction(Production p) {
		this.productions.add(p);
	}
	
	public List<Production> getProductions() {
		return productions;
	}
	
	public boolean isNullable() {
		Symbol lambda = new Terminal("!");
		// si existe una produccion NT -> lambda, es anulable
		for (Production p : productions ) {
			if (p.getSymbols().contains(lambda)) {
				return true;
			}
		}
		return false;
	}
	
	public String toString() {
		return symbol;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((productions == null) ? 0 : productions.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		NonTerminal other = (NonTerminal) obj;
		if (productions == null) {
			if (other.productions != null)
				return false;
		} else if (!productions.equals(other.productions))
			return false;
		return true;
	}
	
	

}
